package cc.rengu.igas.channel.upqc.core.service.trans;


import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.common.util.UpqcSecurityUtil;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.ComInfo;
import cc.rengu.igas.channel.upqc.facade.bean.PayerInfo;
import cc.rengu.igas.channel.upqc.facade.request.C2BPreAuthFinishCancelRequest;
import cc.rengu.igas.channel.upqc.facade.response.C2BPreAuthFinishCancelResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.RandomUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import org.apache.commons.codec.binary.Base64;

import java.util.Map;


/**
 * 被扫预授权完成撤销
 */
public class C2BPreAuthFinishCancelService extends PayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        C2BPreAuthFinishCancelRequest c2BPreAuthFinishCancelRequest = new C2BPreAuthFinishCancelRequest();
        ConvertUtil.convertOutput(c2BPreAuthFinishCancelRequest);
        return c2BPreAuthFinishCancelRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        C2BPreAuthFinishCancelResponse c2BPreAuthFinishCancelResponse = new C2BPreAuthFinishCancelResponse();
        C2BPreAuthFinishCancelRequest c2BPreAuthFinishCancelRequest = (C2BPreAuthFinishCancelRequest) request;
        /* 根据微信支付产品类型对接口接口字段赋值 */
        ConvertUtil.convertInput(c2BPreAuthFinishCancelRequest);

        /* 演示环境发送短信默认返回成功 */
        if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
            ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
            c2BPreAuthFinishCancelResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            c2BPreAuthFinishCancelResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            c2BPreAuthFinishCancelResponse.setSettleKey(RandomUtil.randomNumStr(32));
            c2BPreAuthFinishCancelResponse.setSettleDate(DateUtil.getCurrentDate());
            chnlBizResponse.setResult(c2BPreAuthFinishCancelResponse);
            return chnlBizResponse;
        }

        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, c2BPreAuthFinishCancelRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, c2BPreAuthFinishCancelRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        ConvertUtil.convertOutput(c2BPreAuthFinishCancelResponse);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            rglog.error("调用银联二维码服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            c2BPreAuthFinishCancelResponse.setRespCode("04");
            c2BPreAuthFinishCancelResponse.setRespDesc("交易状态未明");
        } else if (0 != iReturnCode) {
            rglog.error("调用银联二维码服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }

        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(c2BPreAuthFinishCancelResponse.getRespCode())) {
            c2BPreAuthFinishCancelResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            c2BPreAuthFinishCancelResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            if (c2BPreAuthFinishCancelResponse.getSettleDate() != null) {
                c2BPreAuthFinishCancelResponse.setSettleDate(UpqcSecurityUtil.getCupsSettleDate(c2BPreAuthFinishCancelResponse.getSettleDate()));
            }
            String comInfoStr = xmlTreeUtil.getXmlTreeStringValue(UpqcTreeNodeConstant.comInfoStr);
            if (comInfoStr != null) {
                comInfoStr = new String(Base64.decodeBase64(comInfoStr));
                rglog.error("解密的comInfo:<{}>", comInfoStr);
                ComInfo comInfo = new ComInfo();
                Map<String, Object> kvMap = UpqcSecurityUtil.parseKvString(comInfoStr.substring(1, comInfoStr.length() - 1));
                if (kvMap.get("F0") != null) {
                    comInfo.setMsgType((String) kvMap.get("F0"));
                }
                if (kvMap.get("F3") != null) {
                    comInfo.setProcCode((String) kvMap.get("F3"));
                }
                if (kvMap.get("F25") != null) {
                    comInfo.setEntryMode((String) kvMap.get("F25"));
                }
                if (kvMap.get("F37") != null) {
                    comInfo.setReferenceNo((String) kvMap.get("F37"));
                }
                if (kvMap.get("F60") != null) {
                    comInfo.setReserve((String) kvMap.get("F60"));
                }
                c2BPreAuthFinishCancelResponse.setComInfo(comInfo);
            }
        }
        chnlBizResponse.setResult(c2BPreAuthFinishCancelResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        C2BPreAuthFinishCancelResponse c2BPreAuthFinishCancelResponse = (C2BPreAuthFinishCancelResponse) response.getResult();
        ConvertUtil.convertInput(c2BPreAuthFinishCancelResponse);
    }

}
